<template>
  <div class="page-container">
    <div class="page-header">
      <button class="back-button" @click="goToHome">
        <span class="icon-arrow-left">←</span>
        返回首页
      </button>
      <div class="header-content">
        <h2>资源排期</h2>
        <p>管理设备、会议室等资源的使用计划</p>
      </div>
    </div>
    
    <div class="tabs-container">
      <div class="tab" :class="{ active: activeTab === 'equipment' }" @click="activeTab = 'equipment'">
        设备资源
      </div>
      <div class="tab" :class="{ active: activeTab === 'meetingRoom' }" @click="activeTab = 'meetingRoom'">
        会议室
      </div>
      <div class="tab" :class="{ active: activeTab === 'vehicle' }" @click="activeTab = 'vehicle'">
        车辆
      </div>
    </div>
    
    <div class="card">
      <div class="card-header">
        <h3>{{ getTabTitle() }}</h3>
        <button class="btn-primary" @click="showAddResourceModal = true">
          <span class="icon-plus">+</span> 新增资源分配
        </button>
      </div>
      <div class="card-body">
        <div class="table-responsive">
          <table class="data-table">
            <thead>
              <tr>
                <th>资源名称</th>
                <th>类型</th>
                <th>负责人</th>
                <th>开始时间</th>
                <th>结束时间</th>
                <th>状态</th>
                <th>用途</th>
                <th>操作</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="(resource, index) in filteredResources" :key="index" class="table-row">
                <td>{{ resource.name }}</td>
                <td>{{ resource.type }}</td>
                <td>{{ resource.person }}</td>
                <td>{{ formatDateTime(resource.startTime) }}</td>
                <td>{{ formatDateTime(resource.endTime) }}</td>
                <td>
                  <span :class="['status', resource.status]">{{ getStatusText(resource.status) }}</span>
                </td>
                <td>{{ resource.purpose }}</td>
                <td class="action-buttons">
                  <button class="btn-action" @click="editResource(index)">编辑</button>
                  <button class="btn-action btn-danger" @click="deleteResource(index)">删除</button>
                </td>
              </tr>
              <tr v-if="filteredResources.length === 0">
                <td colspan="8" class="empty-state">
                  <div class="empty-icon">🔌</div>
                  <p>暂无{{ getTabTitle() }}的分配记录</p>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>

    <!-- 添加/编辑资源弹窗 -->
    <div v-if="showAddResourceModal" class="modal-backdrop">
      <div class="modal">
        <div class="modal-header">
          <h3>{{ editingIndex !== null ? '编辑资源分配' : '新增资源分配' }}</h3>
          <button class="modal-close" @click="showAddResourceModal = false">&times;</button>
        </div>
        <div class="modal-body">
          <div class="form-group">
            <label>资源名称</label>
            <input type="text" class="form-control" placeholder="请输入资源名称">
          </div>
          <div class="form-group">
            <label>类型</label>
            <select class="form-control">
              <option>笔记本电脑</option>
              <option>投影仪</option>
              <option>小型会议室</option>
              <option>大型会议室</option>
              <option>7座商务车</option>
            </select>
          </div>
          <div class="form-group">
            <label>负责人</label>
            <select class="form-control">
              <option>张三</option>
              <option>李四</option>
              <option>王五</option>
              <option>赵六</option>
            </select>
          </div>
          <div class="form-row">
            <div class="form-group">
              <label>开始时间</label>
              <input type="datetime-local" class="form-control">
            </div>
            <div class="form-group">
              <label>结束时间</label>
              <input type="datetime-local" class="form-control">
            </div>
          </div>
          <div class="form-group">
            <label>用途</label>
            <input type="text" class="form-control" placeholder="请输入使用用途">
          </div>
        </div>
        <div class="modal-footer">
          <button class="btn-secondary" @click="showAddResourceModal = false">取消</button>
          <button class="btn-primary" @click="saveResource">保存</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ResourceSchedule',
  data() {
    return {
      activeTab: 'equipment',
      resources: [
        // 设备资源
        {
          id: 1,
          category: 'equipment',
          name: '笔记本电脑-L001',
          type: '笔记本电脑',
          person: '张三',
          startTime: '2023-10-01 09:00',
          endTime: '2023-10-10 18:00',
          status: 'inUse',
          purpose: '项目开发'
        },
        {
          id: 2,
          category: 'equipment',
          name: '投影仪-P002',
          type: '投影仪',
          person: '李四',
          startTime: '2023-10-05 14:00',
          endTime: '2023-10-05 16:00',
          status: 'available',
          purpose: '客户演示'
        },
        // 会议室
        {
          id: 3,
          category: 'meetingRoom',
          name: '一号会议室',
          type: '小型会议室',
          person: '王五',
          startTime: '2023-10-03 10:00',
          endTime: '2023-10-03 11:30',
          status: 'inUse',
          purpose: '团队周会'
        },
        {
          id: 4,
          category: 'meetingRoom',
          name: '二号会议室',
          type: '大型会议室',
          person: '赵六',
          startTime: '2023-10-06 09:00',
          endTime: '2023-10-06 17:00',
          status: 'available',
          purpose: '培训会议'
        },
        // 车辆
        {
          id: 5,
          category: 'vehicle',
          name: '商务车-C001',
          type: '7座商务车',
          person: '张三',
          startTime: '2023-10-02 08:00',
          endTime: '2023-10-02 18:00',
          status: 'inUse',
          purpose: '客户拜访'
        }
      ],
      showAddResourceModal: false,
      editingIndex: null
    }
  },
  computed: {
    filteredResources() {
      return this.resources.filter(item => item.category === this.activeTab);
    }
  },
  methods: {
    goToHome() {
      this.$router.push('/home');
    },
    getTabTitle() {
      const titles = {
        equipment: '设备资源',
        meetingRoom: '会议室',
        vehicle: '车辆'
      };
      return titles[this.activeTab];
    },
    formatDateTime(dateTimeStr) {
      return new Date(dateTimeStr).toLocaleString('zh-CN');
    },
    getStatusText(status) {
      const map = {
        available: '可用',
        inUse: '使用中',
        maintenance: '维护中',
        reserved: '已预约'
      };
      return map[status] || status;
    },
    editResource(index) {
      this.editingIndex = index;
      this.showAddResourceModal = true;
    },
    deleteResource(index) {
      const resource = this.filteredResources[index];
      if (confirm(`确定要删除${resource.name}的分配记录吗？`)) {
        const originalIndex = this.resources.findIndex(item => item.id === resource.id);
        this.resources.splice(originalIndex, 1);
      }
    },
    saveResource() {
      this.showAddResourceModal = false;
      this.editingIndex = null;
    }
  }
}
</script>

<style scoped>
/* 继承基础样式并添加差异化样式 */
.page-container {
  padding: 24px;
  max-width: 1400px;
  margin: 0 auto;
  background: #f8fafc;
  min-height: 100vh;
}

.page-header {
  margin-bottom: 24px;
  display: flex;
  align-items: center;
  gap: 20px;
}

.header-content h2 {
  margin: 0 0 8px 0;
  color: #1e293b;
  font-size: 24px;
  font-weight: 600;
}

.header-content p {
  margin: 0;
  color: #64748b;
  font-size: 14px;
}

.back-button {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 16px;
  background: #3b82f6;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-weight: 500;
}

.back-button:hover {
  background: #2563eb;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.25);
}

.back-button:active {
  transform: translateY(0);
}

.card {
  background: white;
  border-radius: 10px;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.08);
  margin-bottom: 24px;
  overflow: hidden;
  transition: all 0.3s ease;
}

.card:hover {
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.12);
}

.card-header {
  padding: 16px 24px;
  border-bottom: 1px solid #f1f5f9;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h3 {
  margin: 0;
  color: #1e293b;
  font-size: 18px;
  font-weight: 600;
}

.card-body {
  padding: 24px;
}

/* 过滤区样式 */
.filters-card {
  padding: 16px 24px;
}

.filters-container {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: center;
}

.filter-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.filter-group label {
  color: #334155;
  font-weight: 500;
  font-size: 14px;
}

.filter-group select,
.form-control {
  padding: 9px 12px;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  background: white;
  font-size: 14px;
  transition: all 0.2s ease;
}

.filter-group select:focus,
.form-control:focus {
  outline: none;
  border-color: #3b82f6;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.filter-actions {
  margin-left: auto;
}

/* 统计项样式 */
.header-stats {
  display: flex;
  gap: 24px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 6px;
}

.stat-label {
  color: #64748b;
  font-size: 14px;
}

.stat-value {
  font-weight: 600;
  color: #1e293b;
  font-size: 15px;
}

/* 表格样式 */
.table-responsive {
  overflow-x: auto;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 900px;
}

.data-table th,
.data-table td {
  padding: 14px 16px;
  text-align: left;
  border-bottom: 1px solid #f1f5f9;
  font-size: 14px;
}

.data-table th {
  background-color: #f8fafc;
  font-weight: 600;
  color: #334155;
  position: sticky;
  top: 0;
  z-index: 10;
}

.table-row:hover {
  background-color: #f8fafc;
  transition: background-color 0.2s;
}

/* 优先级和状态标签 */
.priority, .status {
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}

.priority.high {
  background: rgba(239, 68, 68, 0.1);
  color: #dc2626;
}

.priority.medium {
  background: rgba(245, 158, 11, 0.1);
  color: #d97706;
}

.priority.low {
  background: rgba(34, 197, 94, 0.1);
  color: #16a34a;
}

.status.pending {
  background: rgba(148, 163, 184, 0.1);
  color: #64748b;
}

.status.inProgress {
  background: rgba(59, 130, 246, 0.1);
  color: #2563eb;
}

.status.completed {
  background: rgba(34, 197, 94, 0.1);
  color: #16a34a;
}

.status.delayed {
  background: rgba(239, 68, 68, 0.1);
  color: #dc2626;
}

/* 进度条样式 */
.progress-bar {
  height: 8px;
  background-color: #f1f5f9;
  border-radius: 4px;
  overflow: hidden;
  position: relative;
}

.progress-fill {
  height: 100%;
  background-color: #3b82f6;
  transition: width 0.3s ease;
}

.progress-text {
  position: absolute;
  right: 5px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 12px;
  color: #334155;
  font-weight: 500;
}

.text-danger {
  color: #dc2626;
  font-weight: 500;
}

/* 按钮样式 */
.btn-primary {
  background: #3b82f6;
  color: white;
  border: none;
  padding: 9px 18px;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: all 0.3s ease;
  font-weight: 500;
}

.btn-primary:hover {
  background: #2563eb;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.25);
}

.btn-primary:active {
  transform: scale(0.98);
}

.btn-action {
  background: #f1f5f9;
  border: none;
  padding: 6px 12px;
  border-radius: 4px;
  cursor: pointer;
  margin-right: 6px;
  font-size: 13px;
  color: #334155;
  transition: all 0.2s ease;
}

.btn-action:hover {
  background: #e2e8f0;
  transform: translateY(-1px);
}

.btn-action:active {
  transform: scale(0.96);
}

.btn-danger {
  background: rgba(239, 68, 68, 0.1);
  color: #dc2626;
}

.btn-danger:hover {
  background: rgba(239, 68, 68, 0.2);
}

.action-buttons {
  display: flex;
  gap: 6px;
}

/* 空状态样式 */
.empty-state {
  text-align: center;
  padding: 48px 24px;
  color: #64748b;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 16px;
  opacity: 0.7;
}

/* 弹窗样式 */
.modal-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  animation: fadeIn 0.3s;
}

.modal {
  background: white;
  border-radius: 10px;
  width: 90%;
  max-width: 600px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
  animation: slideIn 0.3s;
}

.modal-header {
  padding: 16px 24px;
  border-bottom: 1px solid #f1f5f9;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h3 {
  font-size: 18px;
}

.modal-close {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #64748b;
  transition: color 0.2s, transform 0.2s;
}

.modal-close:hover {
  color: #dc2626;
  transform: rotate(90deg);
}

.modal-body {
  padding: 24px;
}

.form-group {
  margin-bottom: 16px;
}

.form-group label {
  display: block;
  margin-bottom: 6px;
  color: #334155;
  font-weight: 500;
  font-size: 14px;
}

.form-row {
  display: flex;
  gap: 16px;
}

.form-row .form-group {
  flex: 1;
}

.modal-footer {
  padding: 16px 24px;
  border-top: 1px solid #f1f5f9;
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

.btn-secondary {
  background: #f1f5f9;
  color: #334155;
  border: none;
  padding: 9px 18px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s ease;
  font-weight: 500;
}

.btn-secondary:hover {
  background: #e2e8f0;
  transform: translateY(-2px);
}

.btn-secondary:active {
  transform: scale(0.98);
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideIn {
  from { transform: scale(0.95); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

/* 标签页样式 */
.tabs-container {
  display: flex;
  gap: 2px;
  margin-bottom: 24px;
  border-bottom: 1px solid #e2e8f0;
}

.tab {
  padding: 12px 24px;
  background: #f8fafc;
  cursor: pointer;
  border-radius: 6px 6px 0 0;
  transition: all 0.3s ease;
  color: #64748b;
  font-weight: 500;
  position: relative;
}

.tab.active {
  background: white;
  color: #3b82f6;
  margin-bottom: -1px;
}

.tab.active::after {
  content: '';
  position: absolute;
  bottom: -1px;
  left: 0;
  width: 100%;
  height: 3px;
  background: #3b82f6;
  border-radius: 3px 3px 0 0;
}

.tab:hover:not(.active) {
  background: #f1f5f9;
  color: #334155;
}

/* 状态标签样式 */
.status {
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}

.status.available {
  background: rgba(34, 197, 94, 0.1);
  color: #16a34a;
}

.status.inUse {
  background: rgba(59, 130, 246, 0.1);
  color: #2563eb;
}

.status.maintenance {
  background: rgba(245, 158, 11, 0.1);
  color: #d97706;
}

.status.reserved {
  background: rgba(139, 92, 246, 0.1);
  color: #7c3aed;
}
</style>